package com.ruoyi.wx.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wx.config.WxMaConfiguration;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.domain.WxMaUser;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
import cn.binarywang.wx.miniapp.config.WxMaConfig;

@Component
public class WxMaUserService {
	
	@Autowired
	private IWxAppService wxAppService;

	/**
     * 获取用户信息
     */
	public WxMaUser getUserInfo(WxMaForm wxMaForm){
		String openId = null;
		String unionId = null;
		try {
			WxMaService wxMaService = WxMaConfiguration.getMaServices().get(wxMaForm.getAppId());
			if(wxMaService == null){
				wxMaService = new WxMaServiceImpl();
				WxMaConfig config = wxAppService.getAppConfig(wxMaForm.getAppId());
				wxMaService.setWxMaConfig(config);
				// 加载进内存
				WxMaConfiguration.getMaServices().put(wxMaForm.getAppId(), wxMaService);
			}
			
			WxMaJscode2SessionResult res = wxMaService.getUserService().getSessionInfo(wxMaForm.getCode());
			openId = res.getOpenid();
			unionId = res.getUnionid();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("微信授权失败");
		}
		WxMaUser wxMaUser = new WxMaUser();
		wxMaUser.setAppId(wxMaForm.getAppId());
		wxMaUser.setOpenId(openId);
		wxMaUser.setUnionId(unionId);
		return wxMaUser;
	}
	
	/**
     * 获取手机号信息
     */
	public String getOpenPhoneNumberInfo(WxMaForm wxMaForm){
		WxMaPhoneNumberInfo phoneNumberInfo = null;
		try {
			WxMaService wxMaService = WxMaConfiguration.getMaServices().get(wxMaForm.getAppId());
			if(wxMaService == null){
				wxMaService = new WxMaServiceImpl();
				WxMaConfig config = wxAppService.getAppConfig(wxMaForm.getAppId());
				wxMaService.setWxMaConfig(config);
				// 加载进内存
				WxMaConfiguration.getMaServices().put(wxMaForm.getAppId(), wxMaService);
			}
			// 获取手机号信息
			phoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(wxMaForm.getCode());
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("微信获取手机号失败");
		}
		return phoneNumberInfo.getPhoneNumber();
	}
	
	public String getPhoneNumberInfo(WxMaForm wxMaForm){
		WxMaPhoneNumberInfo phoneNumberInfo = null;
		try {
			WxMaService wxMaService = WxMaConfiguration.getMaServices().get(wxMaForm.getAppId());
			if(wxMaService == null){
				wxMaService = new WxMaServiceImpl();
				WxMaConfig config = wxAppService.getAppConfig(wxMaForm.getAppId());
				wxMaService.setWxMaConfig(config);
				// 加载进内存
				WxMaConfiguration.getMaServices().put(wxMaForm.getAppId(), wxMaService);
			}
			
			WxMaJscode2SessionResult res = wxMaService.getUserService().getSessionInfo(wxMaForm.getCode());
			String sessionKey = res.getSessionKey();
			// 获取手机号信息
			phoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, wxMaForm.getEncryptedData(), wxMaForm.getIv());
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("微信获取手机号失败");
		}
		return phoneNumberInfo.getPhoneNumber();
	}

	public Boolean checkMessage(WxMaForm wxMaForm) {
		if(StringUtils.isBlank(wxMaForm.getMessage())){
			return true;
		}
		Boolean checkMessage = false;
		try {
			WxMaService wxMaService = WxMaConfiguration.getMaServices().get(wxMaForm.getAppId());
			if(wxMaService == null){
				wxMaService = new WxMaServiceImpl();
				WxMaConfig config = wxAppService.getAppConfig(wxMaForm.getAppId());
				wxMaService.setWxMaConfig(config);
				// 加载进内存
				WxMaConfiguration.getMaServices().put(wxMaForm.getAppId(), wxMaService);
			}
			
			WxMaMsgSecCheckCheckRequest msgRequest = WxMaMsgSecCheckCheckRequest.builder()
					.version("2")
					.openid(wxMaForm.getOpenId())
					.scene(2)
					.content(wxMaForm.getMessage())
					.build();
			WxMaMsgSecCheckCheckResponse res = wxMaService.getSecCheckService().checkMessage(msgRequest);
			String label = res.getResult().getLabel();
			if("100".equals(label)){
				checkMessage = true;
			}
		} catch (Exception e) {
			throw new ServiceException("微信校验文本失败");
		}
		return checkMessage;
	}

	public Boolean checkImage(String url) {
		WxMaForm wxMaForm = new WxMaForm();
		wxMaForm.setUrl(url);
		Boolean checkImage = false;
		try {
			WxMaService wxMaService = WxMaConfiguration.getMaServices().get(wxMaForm.getAppId());
			if(wxMaService == null){
				wxMaService = new WxMaServiceImpl();
				WxMaConfig config = wxAppService.getAppConfig(wxMaForm.getAppId());
				wxMaService.setWxMaConfig(config);
				// 加载进内存
				WxMaConfiguration.getMaServices().put(wxMaForm.getAppId(), wxMaService);
			}
			
			checkImage = wxMaService.getSecCheckService().checkImage(wxMaForm.getUrl());
		} catch (Exception e) {
			throw new ServiceException("微信校验图片失败");
		}
		return checkImage;
	}
}
